package components;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

class GameClient {

	private Socket socket;
	private BufferedReader reader;
	
	public GameClient() {
		initializeSocket();
		initializeReader();
	}
	
	private void initializeSocket() {
		try {
			socket = new Socket("localhost", 1234);
		} catch (UnknownHostException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (IOException e) {
			e.getMessage();
			e.printStackTrace();
		}
	}
	
	private void initializeReader() {
		try {
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (IOException e) {
			e.getMessage();
			e.printStackTrace();
		}
	}
	
	private boolean isReady() {
		try {
			if(reader.ready()) {
				return true;
			}else {
				return false;
			}
		} catch (IOException e) {
			return false;
		}
	}
	
	private String readLine() {
		String output = null;
		try {
			output = reader.readLine();
		} catch (IOException e) {
			System.err.println("Could not reader from reader!!!!");
			System.exit(-1);
		}	
		return output;
	}
	
	
	public void readFromServer() {
		String role = null;
		RSAKey key = null;
		boolean flag = true;
		boolean ali = true;
		while (true) {
			if(isReady()) {
				String string = readLine();
				System.out.println(string);
				if(string.equals("END_GAME")) {
					System.exit(1);
				}
				if(flag) {
					role = string;
					flag = false;
				}
				else {
					
					
				if(role.equals("Alice")) {
					if(key == null) {
						key = new RSAKey(string);
					}else if(ali) {
						ali = false;
					}else {
						BufferedReader consoleIn =  
								new BufferedReader(new InputStreamReader(System.in));  
						try {
							String in;
							in = consoleIn.readLine();
							PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
							out.println(in);
						} catch (IOException e) {
							e.printStackTrace();
						}  	
				    
					}
					
					
				}else if(role.equals("Bob")) {
					if(key == null) {
						
					}
					
					
					
					
				}else {
					if(key == null) {
						key = new RSAKey(string);
					}else {	
						
						if(string.equals("Guess x coordinate")) {
							BufferedReader consoleIn =  
									new BufferedReader(new InputStreamReader(System.in));  
							try {
								String in;
								in = consoleIn.readLine();
								PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
								out.println(in);
							} catch (IOException e) {
								e.printStackTrace();
							} 
						}else if (string.equals("Guess y coordinate")) {
							BufferedReader consoleIn =  
									new BufferedReader(new InputStreamReader(System.in));  
							try {
								String in;
								in = consoleIn.readLine();
								PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
								out.println(in);
							} catch (IOException e) {
								e.printStackTrace();
							}
						}else {
							try {
							
								PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
								out.println(string);
							} catch (IOException e) {
								e.printStackTrace();
							}  	
						}
					}
					
					
				}
				}
			}
			
		}
	}
	
	public static void main(String[] args) {
		GameClient client = new GameClient();
		client.readFromServer();
	}
}
